package com.smritimap.demo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by zxn on 2024/12/17.
 */
public class ArcAngleArrowView extends View {
    private Paint mPaint;

    /**
     * 圆的宽度
     */
    private int mCircleWidth = 3;

    public ArcAngleArrowView(Context context) {
        this(context, null);
    }

    public ArcAngleArrowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArcAngleArrowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mCircleWidth);
        mPaint.setColor(Color.CYAN);

        /**
         * 这是一个居中的圆
         */
        /*float x = (getWidth() - getHeight() / 2) / 2;
        float y = getHeight() / 4;
        RectF oval = new RectF(x, y, getWidth() - x, getHeight() - y);*/
        float cX = getWidth() / 2F;
        float cY = getHeight() / 2F;
        float arcR = 60;

        float left = cX - arcR;
        float top = cY - arcR;
        float right = cX + arcR;
        float bottom = cY + arcR;

        RectF oval = new RectF(left, top, right, bottom);
        //drawArc(canvas, oval, -90, 90, false, mPaint);
        //onDrawArrow(canvas, cX + arcR, cY);
//        onDrawArcAngleArrow(canvas,
//                oval,
//                cX + arcR,
//                cY);

        //onDrawArcAngleArrow(canvas, oval);
        onDrawArcAngleArrowByCanvas(canvas, oval);
    }

    /**
     * 绘制圆弧
     *
     * @param oval       定义的圆弧的形状和大小的范围
     * @param startAngle 这个参数的作用是设置圆弧是从哪个角度来顺时针绘画的
     * @param sweepAngle 这个参数的作用是设置圆弧扫过的角度 ，我们从上面的代码就可以知道其中的作用了。
     * @param useCenter  这个参数的作用是设置我们的圆弧在绘画的时候，是否经过圆心、
     * @param paint      这个参数的作用是设置我们的画笔对象的属性
     */
    public void drawArc(Canvas canvas, RectF oval, float startAngle, float sweepAngle, boolean useCenter,
                        Paint paint) {
        canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
    }

    private void onDrawArrow(Canvas canvas, float endX, float endY) {
        Path path = new Path();
        path.reset();
        float arrowLength = 50;
        path.moveTo(endX, endY);
        path.lineTo(endX - arrowLength, endY - arrowLength);

        path.moveTo(endX, endY);
        path.lineTo(endX + arrowLength, endY - arrowLength);
        path.close();
        canvas.drawPath(path, mPaint);
    }

    /**
     * 绘制一个带弧度的箭头、
     *
     * @param canvas
     * @param oval
     * @param endX
     * @param endY
     */
    private void onDrawArcAngleArrow(Canvas canvas,
                                     RectF oval,
                                     float endX,
                                     float endY) {

        Path path = new Path();
        path.reset();
        path.arcTo(oval, -90, 90);

        float arrowLength = 20;
        path.moveTo(endX, endY);
        path.lineTo(endX - arrowLength, endY - arrowLength);

        path.moveTo(endX, endY);
        path.lineTo(endX + arrowLength, endY - arrowLength);
        path.close();
        canvas.drawPath(path, mPaint);
    }

    private void onDrawArcAngleArrow(Canvas canvas,
                                     RectF oval) {

        Path path = new Path();
        path.reset();
        path.arcTo(oval, -90, 90);
        float endX = oval.right;
        float endY = oval.centerY();

        canvas.rotate(90, oval.centerX(), oval.centerY());

        float arrowLength = 20;
        path.moveTo(endX, endY);

        path.lineTo(endX - arrowLength, endY - arrowLength);

        path.moveTo(endX, endY);
        path.lineTo(endX + arrowLength, endY - arrowLength);
        path.close();
        canvas.drawPath(path, mPaint);
    }

    private void onDrawArcAngleArrowByCanvas(Canvas canvas, RectF oval) {
        canvas.drawArc(oval, -90, 90, false, mPaint);
        float endX = oval.right;
        float endY = oval.centerY();
        canvas.translate(0,15);
        canvas.rotate(-8, oval.centerX(), oval.centerY());
        float arrowLength = 20;
        canvas.drawLine(endX, endY,endX - arrowLength, endY - arrowLength,mPaint);
        canvas.drawLine(endX, endY,endX + arrowLength, endY - arrowLength,mPaint);
    }


}
